﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using System.Windows;
using System.Windows.Controls;

namespace MapSurface.WidgetSurface
{
    public class XamlObjectManager
    {
        #region Properties
        public delegate void UpdateDisplay(object uielement);
        private UpdateDisplay update = null;
        private XamlObjects _designRoot;
        public ScatterViewItem _deleteMess;

        public XamlObjects DesignRoot
        {
            get { return _designRoot; }
            set { _designRoot = value; }
        }
        private System.Windows.Controls.Canvas _designSpace; 

        #endregion

        #region Constructor
        public XamlObjectManager(System.Windows.Controls.Canvas designSpace,  string dllDir, string apiFile, string choiceFile,List<Delegate> selectHanlder)
        {
            _designSpace = designSpace;
            BorderElement.BorderCollection = new List<BorderElement>();
            _designRoot = new XamlObjects(choiceFile, dllDir, apiFile, selectHanlder,_designSpace);
            update += udate;
        }
        #endregion


        #region Operation
        public int replaceObject(ref object rootUi, object newObject, object instance, bool all)
        {
            EventHandler replace = ReplaceUIElementHappened;
            if (replace != null)
            {
                object[] sender = { instance,  _designRoot.widgetChoice.findUIElement(newObject), all };
                replace(sender, null);
                //Replace in fui model
                _designRoot.replaceObject(ref rootUi, newObject, instance, all);

            }
            return 0;
        }
        private void udate(object o)
        {
        }
        public void deleteXamlObject(object instance)
        {
            EventHandler delete = DeleteUIElementHappened;
            if (delete != null)
            {
                delete(instance, null);
                object root = _designSpace;
                _designRoot.deleteXamlElement(ref root, instance);
                _designSpace = (System.Windows.Controls.Canvas)root;

            }
        }
        public void checkDeleteXamlObject(object instance)
        {
            EventHandler checker = CheckDeleteHappened;
            if (checker != null)
            {
                checker(instance, null);
            }
        }
 
        public List<Object> getEquivalentChoice(object instance)
        {
           return _designRoot.getChoiceInstance(instance);
        }
        public void saveFinalUI(string fileName, Object current)
        {
            _designRoot.concretise(fileName,current);
        }
        #endregion

        #region Observable Event

        public event EventHandler DeleteUIElementHappened;
        public event EventHandler ReplaceUIElementHappened;
        public event EventHandler CheckDeleteHappened;
        #endregion

        #region UIModel Observer Handler

        public void UiModelLoaderHandler(object sender, EventArgs args)
        {
            try
            {
                _designSpace.Children.Clear();
                List<Object> list = _designRoot.build((UIModelElement.UIModel)sender);
                foreach (Object o in list)
                {
                    _designSpace.Children.Add((System.Windows.UIElement)o);
                    ((FrameworkElement)o).Margin = new Thickness(10);
                    ((FrameworkElement)o).Height = 650;
                    ((FrameworkElement)o).Width = 650;
                    ((FrameworkElement)((BorderElement)o).Content.Children[0]).Width = 600;
                    ((FrameworkElement)((BorderElement)o).Content.Children[0]).Height = 600;
                }
            }
            catch (InvalidCastException)
            {

            }
        }

        public void ModelMessageHandler(object sender, EventArgs e)
        {
            if (sender != null && sender.ToString().Length != 0)
            {
                _designRoot.message.Text = sender.ToString();
                
            }
            else
            {
                _designRoot.message.Text = "No link with FC";
            }
            _deleteMess.Visibility = Visibility.Visible;
        }
        #endregion
    }
}
